var path = require('path');
// eslint-disable-next-line no-undef
const filePath = path.join(__dirname, '/img/');
console.log(filePath);
class Common {
    /**
     * 生成订单号
     * @param j
     * @returns {*|string}
     */
    randomNo(j) {
        var randomNo = '';
        for (var i = 0; i < j; i++) {
            randomNo += Math.floor(Math.random() * 10);
        }
        randomNo = new Date().getTime() + randomNo;
        return randomNo;
    }

    /**
     * ip
     * @returns {address|{defaultValue, comment, allowNull, type}}
     */
    getIPAddress() {
        var interfaces = require('os').networkInterfaces();
        for (var devName in interfaces) {
            var face = interfaces[devName];
            for (var i = 0; i < face.length; i++) {
                var alias = face[i];
                if (
                    alias.family === 'IPv4' &&
                    alias.address !== '127.0.0.1' &&
                    !alias.internal
                ) {
                    return alias.address;
                }
            }
        }
    }

    /**
     * 生成二维码
     * @param code
     * @returns {string}
     */
    createCode(code) {
        try {
            var qr = require('qr-image');
            var qrSvg = qr.image(code, { type: 'png' });
            qrSvg.pipe(
                require('fs').createWriteStream(filePath + code + '.png')
            );
            const src =
                'http://192.168.0.130/car/src/utils/img/' + code + '.png';
            return src;
        } catch (e) {
            return false;
        }
    }
}
module.exports = new Common();
